package com.anolesoft.yr.mbus.code.controller;

import com.anolesoft.common.core.constant.HttpStatus;
import com.anolesoft.common.core.web.domain.AjaxResult;
import com.anolesoft.yr.mbus.base.vo.BaseAddressBookVO;
import com.anolesoft.yr.mbus.code.dto.CodeItemRelDTO;
import com.anolesoft.yr.mbus.code.service.CodeItemRelService;
import com.anolesoft.yr.mbus.code.vo.CodeItemRelVO;
import com.anolesoft.yr.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @author zhj
 * @date 2020/9/22 19:26
 */
@RestController
@RequestMapping("/api/code/codeitemrel")
public class CodeItemRelController {
    @Autowired
    private CodeItemRelService codeItemRelService;

    @PostMapping(value = "/add", produces = MediaType.APPLICATION_JSON_VALUE)
    public AjaxResult create(CodeItemRelVO codeItemRelVO) {
        CodeItemRelDTO codeItemRelDTO = vo2dto(codeItemRelVO, true);
        return new AjaxResult(HttpStatus.SUCCESS, Constants.CREATE_SUCCESS_MESSAGE, codeItemRelService.create(codeItemRelDTO));
    }
    @PutMapping(value = "/update", produces = MediaType.APPLICATION_JSON_VALUE)
    public AjaxResult modify(CodeItemRelVO codeItemRelVO) {
        CodeItemRelDTO codeItemRelDTO = vo2dto(codeItemRelVO, false);
        return new AjaxResult(HttpStatus.SUCCESS, Constants.MODIFY_SUCCESS_MESSAGE, codeItemRelService.modify(codeItemRelDTO));
    }

    @DeleteMapping(value = "/delete/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
    public AjaxResult remove(@PathVariable("id") String id) {
        return new AjaxResult(HttpStatus.SUCCESS, Constants.DELETE_SUCCESS_MESSAGE, codeItemRelService.remove(id));
    }
    @DeleteMapping(value = "/delete", produces = MediaType.APPLICATION_JSON_VALUE)
    public AjaxResult remove(@RequestParam("ids[]") List<String> ids) {
        return new AjaxResult(HttpStatus.SUCCESS, Constants.DELETE_SUCCESS_MESSAGE, codeItemRelService.removeBatch(ids));
    }
    @GetMapping(value = "/get/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
    public AjaxResult selectOneById(@PathVariable("id") String id) {
        Optional<CodeItemRelDTO> codeItemRelDTO = this.codeItemRelService.selectOneById(id);
        if (codeItemRelDTO.isPresent()) {
            return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE, CustomBeanUtils.codeItemRelDTO2VO(codeItemRelDTO.get()));
        } else {
            return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE, null);
        }
    }
    @GetMapping(value = "/list/codemainuid", produces = MediaType.APPLICATION_JSON_VALUE)
    public AjaxResult selectList(@RequestParam("codeMainUid") String codeMainUid) {
        List<CodeItemRelVO> collect = this.codeItemRelService.selectListByCodeMainUid(codeMainUid).stream().map(CustomBeanUtils::codeItemRelDTO2VO).collect(Collectors.toList());
        PageInfo<List<CodeItemRelVO>> pageInfo = new PageInfo<>(collect.size(), collect);
        return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE, pageInfo);
    }

    private CodeItemRelDTO vo2dto(CodeItemRelVO codeItemRelVO, boolean newId) {
        return CodeItemRelDTO.builder()
                .createTime(LocalDateTime.now())
                .createUserUid(SecurityUtils.getUserId())
                .deleteFlag(Boolean.FALSE)
                .factoryCode(SecurityUtils.getFactoryCode())
                .codeMainUid(codeItemRelVO.getCodeMainUid())
                .codeItemUid(codeItemRelVO.getCodeItemUid())
                .orderNum(codeItemRelVO.getOrderNum())
                .modifyTime(LocalDateTime.now())
                .modifyUserUid(SecurityUtils.getUserId())
                .objuid(newId ? CommonUtils.fastUUID() : codeItemRelVO.getObjuid())
                .remark(codeItemRelVO.getRemark())
                .build();
    }
}
